/**************************************************************************************
 *  Copyright (c)  2010  Pablo Echeverri.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data;

public class ResearchTree
{
	private LinkedList<LinkedList<Technology>> technologies=new LinkedList<LinkedList<Technology>>();
	private LinkedList<String> researchTypes= new LinkedList<String>();
	
	
	
	/**************************************************************************************
	 * Author: Pablo Echeverri
	 * Research tree
	 **************************************************************************************/

	public ResearchTree()
	{
		
		
	}
	
	/**************************************************************************************
	 * returns the Technology object when given the type (unit,structure,..) and the
	 * name of the technology (attack strength,defense strength,...) 
	 * if not found, returns null
	 **************************************************************************************/
	public Technology getTechnology(String type, String name)
	{
		this.researchTypes.setToFirst();
		this.technologies.setToFirst();
		
		LinkedList<Technology> perType=this.technologies.peekCurrent(); //vertical list
		perType.setToFirst();
		
		for(int i=0; i<researchTypes.getSize();i++)
		{
			if(this.researchTypes.peekCurrent().equals(type))
			{
				for(int j=0; i<perType.getSize();j++)
				{
					if(perType.peekCurrent().getName().equals(name))
					{
						return perType.peekCurrent();
					}
					perType.next();
				}
			}
			else
			{
				this.technologies.next();
				this.researchTypes.next();
				perType=this.technologies.peekCurrent();
				perType.setToFirst();
			}
			
		}
		return null;
	}
	
	/**************************************************************************************
	 * Author: Pablo Echeverri
	 * adds a type of unit/structure to develop research for
	 **************************************************************************************/
	public void addType(String type)
	{
		this.researchTypes.addBack(type);
		LinkedList<Technology> blank=new LinkedList<Technology>();
		this.technologies.addBack(blank);
	}
	/**************************************************************************************
	 * Author: Pablo Echeverri
	 * adds a technology to a given type of structure/unit 
	 * returns true if technology was added successfully (type was found)
	 * else returns false
	 **************************************************************************************/
	public boolean addTechnology(String type, String name,int max_level, double increase_rate, int multiplier)
	{
		this.researchTypes.setToFirst();
		this.technologies.setToFirst();
	
		for(int i=0; i<researchTypes.getSize();i++)
		{
			if(this.researchTypes.peekCurrent().equals(type))
			{
				this.technologies.peekCurrent().addBack(new Technology(name,max_level,increase_rate,multiplier));
				return true;
			}
			this.researchTypes.next();
			this.technologies.next();
		}
		return false;
	}
	public int getTechnologyImprovement(String type, String name)
	{
		Technology currentTech = getTechnology(type, name);
		if(currentTech!=null)
		{
			return currentTech.getImprovement();
		}
		else
		{
			return 0;
		}
	}
	
	public LinkedList<LinkedList<Technology>> getTree()
	{
		return this.technologies;
	}
	
	public LinkedList<String> getTypes()
	{
		return this.researchTypes;
	}
	
	public LinkedList<Technology> getBranch(String type)
	{
		this.researchTypes.setToFirst();
		this.technologies.setToFirst();
		
		for(int i=0;i<this.researchTypes.getSize();i++)
		{
			if(this.researchTypes.peekCurrent().equals(type))
			{
				return this.technologies.peekCurrent();
			}
			this.researchTypes.next();
			this.technologies.next();
		}
		return null;
		
	}
}
	